<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>销售人员</title>
		<link rel="stylesheet" href="/dvmini/res/plugins/layui/css/layui.css">
		<link rel="stylesheet" href="/dvmini/res/css/Layuiglobal.css">
		<link rel="stylesheet" href="/dvmini/res/plugins/font-awesome/css/font-awesome.min.css">
		<link rel="stylesheet" href="http://at.alicdn.com/t/font_191348_aee57zbh6jyk3xr.css">
		<link rel="stylesheet" href="/dvmini/res/css/basic.css">	
	</head>
	<body class="wrap">
		<div class="layui-form">
			<div class="layui-form-item searchbox" style="margin-bottom: 0px;">
				 <div class="layui-input-inline" style="width: 140px;">
					<select name="isRedirectStaff">
						<option value="">是否转人工客服</option>
						<option value="0">否</option>
						<option value="1">是</option>
					</select>
				</div> 
				<div class="layui-input-inline" style="width: 140px;">
					<input type="text" class="layui-input" name="keyword" placeholder="关键字" />
				</div>
			<button class="layui-btn" lay-submit lay-filter="querybtn">查询</button>
				<button class="layui-btn layui-btn-primary resetBtn" >重置</button> 
				<a class="layui-btn layui-btn-warm createBtn" href="#"><i class="layui-icon">&#xe61f;</i> 添加关键字</a>
			</div>
		</div>
		<table class="layui-table">
			<thead>
				<tr>
					<th style="width: 30px;">No</th>
					<th style="min-width:120px;">关键字</th>
					<th style="min-width:100px;">是否转人工客服</th>
					<th style="min-width:140px;">回复内容</th>
					<th style="min-width:80px;">操作</th>
				</tr>
			</thead>
				
			<tbody id="tablelist">
			</tbody>
		</table>
		<div class="nulldata">暂无数据</div>
		<div id="page"></div>
		
	</body>
	<div class="popupbox" style="display:none;">
			<div class="popupbos">
				<div class="layui-form">
					<div class="layui-form-item">
						<label class="layui-form-label"><i style="color:red;margin-right:5px;">*</i>关键词：</label>
						<div class="layui-input-inline" style="width:250px;">
							<input type="text" name="keywords" lay-verify="required" placeholder="关键词" autocomplete="off" id="text1" class="layui-input" value="">
						</div>
						<div class="layui-form-mid layui-word-aux">多个关键词请用英文“ , ”分隔</div>
					</div>
					<form class="layui-form">
						<div class="layui-form-item">
							<label class="layui-form-label"><i style="color:red;margin-right:5px;">*</i>是否转人工客服：</label>
							<div class="layui-input-inline">
								<input type="radio" name="isRedirectStaff" lay-filter="isRedirectStaff" value="0" title="否" checked="ture">
								<input type="radio" name="isRedirectStaff" lay-filter="isRedirectStaff" value="1" title="是" >
							</div>
						</div>
					</form>
					
					<div class="layui-form-item layui-form-text Redirectbox">
						<label for="" class="layui-form-label"><i style="color:red;margin-right:5px;">*</i>回复内容：</label>
						<div class="layui-input-inline" style="width:65%;">
							<textarea class="layui-textarea" name="content" lay-verify="required" id="saytext" placeholder="请输入内容" style="height:100px;" ></textarea>
							<i class="iconfont icon-xiaolian emotion"></i><div class="layui-form-mid layui-word-aux" style="padding-top:10px;line-height:28px;">多条回复内容用“ | ”分隔</div>
						</div>
						<div class="layui-form-mid layui-word-aux " style="margin-left:150px;">注: 活动结束或过期后会自动停止推送</div>
					</div>

				</div>
				

			</div>
		</div>
	<script src='/dvmini/res/js/jquery.min.js'></script>
	<script type="text/javascript" src="/dvmini/res/js/jquery.qqFace.js"></script>
	<script src="/dvmini/res/plugins/layui/layui.js" charset="utf-8"></script>
	<script src="/dvmini/res/js/me.js"></script>
	<script>
	layui.use(['form', 'element', 'laydate', 'layer', 'laypage'], function() {
		var form = layui.form(),
			element = layui.element(),
			laydate = layui.laydate,
			layer = layui.layer,
			laypage = layui.laypage;
			var queryObj = {
					page: 1,
					keyword: null,
					isRedirectStaff: null
				} //查询条件
			//查询
			 form.on('submit(querybtn)', function(data){
				 queryObj=data.field;
				 queryObj.page=1;
			      initdata(queryObj);
			      form.render();
			  });
			 $(".resetBtn").on("click",function(){
				$("input[name=keyword]").val("");
				$("select[name=isRedirectStaff]").val("");
				form.render();
				queryObj.keyword=null;
				queryObj.isRedirectStaff=null;
				 initdata(queryObj);
				
			}) 
			//初始化数据
			initdata(queryObj);
			function initdata(queryObj){
				
				$.post("/dvmini/autoreply/getAutoreplyList",queryObj,function(data){
					data=data.data;
					console.log(data)
					  laypage({
						cont: 'page',
						pages: data.totalPages, //总页数
						curr: queryObj.page,
						skip: true,
						jump: function(obj, first) {
							if(!first) {
								queryObj.page = obj.curr
								initdata(queryObj);
							}

						}
					});  
					var str="";
					if(data.rows.length<1){
						$(".nulldata").show();
					}else{
						$(".nulldata").hide();
						for(var i=0;i<data.rows.length;i++){
							str+='<tr data-id='+ data.rows[i].id +'>'
								+'<td>'+ ((data.page - 1) * data.pageSize + i + 1) +'</td>'
								+'<td style="text-align:left;">'+ data.rows[i].keywords+'</td>'
								+'<td>'+ autoreply(data.rows[i].isredirectstaff) +'</td>'
								+'<td style="text-align:left;">'+ data.rows[i].contents +'</td>'
								+'<td>'
								+'<a href="#" class="layui-btn layui-btn-mini updataBtn">修改</a>'
								+'<a href="#" class="layui-btn layui-btn-mini deleteBtn">删除</a>'
								+'</td>'
								+'</tr>';
						}
					}
					
					$("#tablelist").html(str);
					
				})
			}
			//添加关键字
		$(document).on("click",".createBtn",function(){
			layer.open({
				type:1,
				title:'添加关键字',
				area:["650px","450px"],
				content:$(".popupbox"),
				btnAlign:"c",
				btn:["提交","取消"],
				yes:function(index,layero){
					var keywords=$("input[name=keywords]").val();
					var isRedirectStaff=$("input[name=isRedirectStaff]:checked").val();
					var content="";
					if(keywords==""){
						top.layer.msg("请填写关键词");
						return false;
					}
					if(isRedirectStaff=="0"){
						isRedirectStaff=false;
						content=$("textarea[name=content]").val();
						if(content==""){
							top.layer.msg("请填写回复内容");
							return false;
						}
					}else if(isRedirectStaff=="1"){
						isRedirectStaff=true;
					}
					$.post("/dvmini/autoreply/addAutoreply",{
						keywords:keywords, 
						isRedirectStaff:isRedirectStaff,
						content:content
					},function(data){
						if(data.code==0){
							top.layer.msg("添加成功");
							layer.close(index);
						    queryObj.page=1;
						    initdata(queryObj);
						}
					}); 
					 form.render();
				},
				btn2:function(index,layero){
					layer.close(index);
				},
				 cancel: function(index,layero){ 
					    layer.close(index);
				}
			})
		})
			//修改关键字
		$(document).on("click",".updataBtn",function(){
			var id=$(this).parents("tr").attr("data-id");
			$.post("/dvmini/autoreply/getAutoReply",{id:id},function(data){
				console.log(data)
				$("input[name=keywords]").val(data.data.keywords);
				if(data.data.isredirectstaff==true){
					$("input[name=isRedirectStaff]").eq(1).attr("checked",true);
				}else{
					$("input[name=isRedirectStaff]").eq(0).attr("checked",true);
				}
				$("textarea[name=content]").val(data.data.contents);
				form.render();
				
			})
			
			layer.open({
				type:1,
				title:'修改关键字',
				area:["650px","450px"],
				content:$(".popupbox"),
				btnAlign:"c",
				btn:["提交","取消"],
				yes:function(index,layero){
					var keywords=$("input[name=keywords]").val();
					var isRedirectStaff=$("input[name=isRedirectStaff]:checked").val();
					var content="";
					if(keywords==""){
						top.layer.msg("请填写关键词");
						return false;
					}
					if(isRedirectStaff=="0"){
						isRedirectStaff=false;
						content=$("textarea[name=content]").val();
						if(content==""){
							top.layer.msg("请填写回复内容");
							return false;
						}
					}else if(isRedirectStaff=="1"){
						isRedirectStaff=true;
					}
					$.post("/dvmini/autoreply/updateAutoreply",{
						id:id,
						keywords:keywords, 
						isRedirectStaff:isRedirectStaff,
						content:content
					},function(data){
						if(data.code==0){
							top.layer.msg("修改成功");
							layer.close(index);
						    queryObj.page=1;
						    initdata(queryObj);
						}
					});
					 form.render();
				},
				btn2:function(index,layero){
					layer.close(index);
				},
				 cancel: function(index,layero){ 
					    layer.close(index);
				}
			})
		})
		
		//删除项目
		$(document).on("click", ".deleteBtn", function() {
			var id=$(this).parents("tr").attr("data-id");
			layer.confirm('您确定要删除该关键字吗？', {
				btn: ['确定', '取消'] //按钮
			}, function(index, layero) {
				$.post( "/dvmini/autoreply/deleteAutoreply",{id:id}, function(data) {
					console.log(data)
					if(data.code == 0) {
						top.layer.msg("删除成功");
						queryObj.page=1;
						initdata(queryObj)
						layer.close(index);
					}else{
						top.layer.msg(data.msg);
					}
				})
			}, function(index, layero) {
				layer.close(index);
			});
		});
		form.render() ;
		//显示自动回复信息
	function getReplyInfo() {
			$.post( "http", {
				data:data
			}, function(data) {
				if(data.data!== null) {
					console.log(data)
					$('textarea[name=attention]').html(replace_em(data.data.content));
				}
			})
		}

		//获取自动回复类型
		function getMenuType() {
			var value = "";
			var radio = $("input[name=isRedirectStaff]");
			for(var i = 0; i < radio.length; i++) {
				if(radio[i].checked == true) {
					value = radio[i].value;
					break;
				}
			}
			return value;
		}
	
		
		/*是否转人工客服切换*/
		form.on('radio(isRedirectStaff)', function(data) {
			if(this.value == 1) {
				$('.Redirectbox').hide();
			} else {
				$('.Redirectbox').show();
			}
		});

		//切换自动回复类型
		form.on('radio(keytype)', function(data) {
			if(this.value == 1) {
				$('.Redirectbox').hide();
				$('radio(isRedirectStaff)').attr("checked",true);
				form.render();
			} else {
				$('.Redirectbox').show();
				$('radio(isRedirectStaff)').attr("checked",false);
				form.render();
				
			}
		});
				
			

	})
		function nullToSpace (v) {
		　　 return  !v && v != '0' ? '' : v;
		}
	function autoreply(v){
		if(v!==null&&v!==""){
			if(v==true){
				return "是";
			}else{
				return "否";
			}
		}else{
			return "";
		}
	}
	
	$(function(){
		$('.emotion').qqFace({
			id : 'facebox', 
			assign:'saytext', 
			path:'/dvmini/res/arclist/'	//表情存放的路径
	
		});
	});
	</script>

</html>